package com.loveshare.imgshow.controller;

import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.loveshare.common.util.HttpClientIpUtil;
import com.loveshare.common.util.HttpRequest;
import com.loveshare.imgshow.entity.Doubanmz;
import com.loveshare.imgshow.entity.VideoListInfo;
import com.loveshare.imgshow.service.ImgShowService;
import com.loveshare.log.bean.UservisitlogInfo;
import com.loveshare.log.service.LogService;

@Controller
@RequestMapping(value="/m")
public class MobileImgShowControoler {
	@Autowired
	private ImgShowService imgShowService;
	
	@Autowired
	private LogService logService;

	/**
	 * 表情包
	 * @return
	 */
	@RequestMapping(value="/doubi",method=RequestMethod.GET)
	public String mzImgShow(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="-1") int type,ModelMap map,HttpServletRequest request){
		String ip=HttpClientIpUtil.getIP(request);
		UservisitlogInfo ipInfo=logService.getUservisitlogInfo(ip);
		String ipInfoStr="";
		if(ipInfo!=null){
			if(ipInfo.getIpregion().equals("因为接口不稳定暂时不记录了")){
				ipInfoStr=HttpRequest.getIpRegion(ip);
			}else{
				ipInfoStr=ipInfo.getIpregion();	
			}
		}else{
			ipInfoStr=HttpRequest.getIpRegion(ip);
		}
		logService.addArticleInfo(ip,ipInfoStr,2);
		//随机显示页数
		Random ra =new Random();
		System.out.println("==========有人访问："+ipInfoStr+"-"+ip);
		page=ra.nextInt(2000);
		if(page==0){
			page=1;
		}
		List<Doubanmz> douBanMzs=imgShowService.getDoubanmzs("BQBPic",type, page, 30);
		System.out.println("查询完毕");
		map.put("douBanMzs", douBanMzs);
		map.put("page", page);
		
		return "mobile/doubi";
	}
	/**
	 * 妹子自拍手机端首页
	 * @return
	 */
	@RequestMapping(value="/c-c-",method=RequestMethod.GET)
	public String mzImgShow_Mz(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="-1") int type,ModelMap map,HttpServletRequest request){
		String ip=HttpClientIpUtil.getIP(request);
		UservisitlogInfo ipInfo=logService.getUservisitlogInfo(ip);
		String ipInfoStr="";
		if(ipInfo!=null){
			if(ipInfo.getIpregion().equals("因为接口不稳定暂时不记录了")){
				ipInfoStr=HttpRequest.getIpRegion(ip);
			}else{
				ipInfoStr=ipInfo.getIpregion();	
			}
		}else{
			ipInfoStr=HttpRequest.getIpRegion(ip);
		}
		logService.addArticleInfo(ip,ipInfoStr,1);
		//随机显示页数
		Random ra =new Random();
		System.out.println("==========有人访问："+ipInfoStr+"-"+ip);
		page=ra.nextInt(500);
		if(page==0){
			page=1;
		}
		List<Doubanmz> douBanMzs=imgShowService.getDoubanmzs("DouBanMz_QN",1, page, 30);
		System.out.println("查询完毕");
		map.put("douBanMzs", douBanMzs);
		map.put("page", page);
		
		return "mobile/dbmz";
	}
	/**
	 * 老司机视频区
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/video",method=RequestMethod.GET)
	public String mzImgShow(ModelMap map, HttpServletRequest request){
		String ip=HttpClientIpUtil.getIP(request);
		UservisitlogInfo ipInfo=logService.getUservisitlogInfo(ip);
		String ipInfoStr="";
		if(ipInfo!=null){
			if(ipInfo.getIpregion().equals("因为接口不稳定暂时不记录了")){
				ipInfoStr=HttpRequest.getIpRegion(ip);
			}else{
				ipInfoStr=ipInfo.getIpregion();	
			}
		}else{
			ipInfoStr=HttpRequest.getIpRegion(ip);
		}
		logService.addArticleInfo(ip,ipInfoStr,3);
		
		List<VideoListInfo> videoListInfos=imgShowService.getVideoListInfo(1, 100);
		map.put("videos", videoListInfos);
		return "mobile/video";
	}
	
	// 音乐接口
	@RequestMapping(value="/music",method=RequestMethod.GET)
	public String music(){
		return "mobile/music";
	}
}
